﻿using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices;
using Shop.Model.Dto.Car;
using Shop.Model.Dto.Goo;
using Shop.Model.View.Car;

namespace Shop.WebApi.Controllers;

/// <summary>
/// 购物车
/// </summary>
[ApiController]
[Route("Api/[controller]")]
public class ShopCartController:ShopControllerBase
{
    private readonly IShopCartAppService _cartAppService;
    private readonly IUserContext _userContext;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="cartAppService"></param>
    /// <param name="userContext"></param>
    public ShopCartController(IShopCartAppService cartAppService,IUserContext userContext)
    {
        _cartAppService = cartAppService;
        _userContext = userContext;
    }
    
    /// <summary>
    /// 添加商品到购物车
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("ProductAdd")]
    public AppResult<bool> ProductAdd(ProductAddDto dto)
    {
        try
        {
            return _cartAppService.AddProduct(_userContext.Id, dto);
        }
        catch (Exception ex)
        {
            return Problem(ex.Message.ToString());
        }
        
    }

    /// <summary>
    /// 删除购物车商品
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("DeleteProduct")]
    public  AppResult<bool> DeleteProduct(DeleteProductDto dto)
    {
        return _cartAppService.DeleteProduct(_userContext.Id, dto);
    }

    /// <summary>
    /// 修改购物车商品规格
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("UpdateProduct")]
    public AppResult<bool> UpdateProduct(ProductModifyDto dto)
    {
        try
        {
            return _cartAppService.UpdateProduct(_userContext.Id, dto);
        }
        catch (Exception ex)
        {
            return Problem(ex.Message.ToString());
        }
    }

    /// <summary>
    /// 修改购物车商品数量
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost("UpdateCount")]
    public AppResult<bool> UpdateCount(UpdateCountDto dto)
    {
        try
        {
            return _cartAppService.UpdateCount(_userContext.Id, dto);
        }
        catch (Exception ex)
        {
            return Problem(ex.Message.ToString());
        }
    }

    /// <summary>
    /// 商品规格集合
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("GooSize")]
    public AppResult<GooSizeVo> GooSize([FromQuery] GooSizeDto dto)
    {
        return _cartAppService.GooSize(_userContext.Id, dto);
    }

    /// <summary>
    /// 获取用户购物车商品信息
    /// </summary>
    /// <param name="language"></param>
    /// <returns></returns>
    [HttpGet("list")]
    public async Task<PageModel<ProductListVo>> GetProductList([FromQuery]PageSearchModel<string> language)
    {
        return await _cartAppService.GetProductList(_userContext.Id, language);
    }
}